package com.zlt.bkparking.utils;

import io.swagger.models.auth.In;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class StringUtil {

	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str) {
		return str == null || "".equals(str);
	}
	
	/**
	 * 判断字符串不能为空
	 * @param str
	 * @return
	 */
	public static boolean isNotNull(String str) {
		return str != null && !"".equals(str);
	}
	
	private static final String STRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	
	/**
	 * 随机生成指定长度的字符串
	 * @param len
	 * @return
	 */
	public static String randomStr (int len) {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < len; i++) {
			int index = (int) (Math.random() * STRING.length());
			builder.append(STRING.charAt(index));
		}
		return builder.toString();
	}

	/**
	 * 获取一个uuid字符串
	 * @return
	 */
	public static String uuid (){
		return UUID.randomUUID().toString();
	}

	/**
	 * 将图片加密到base64的字符串
	 * @param bufferedImage
	 * @return
	 */
	public static String changeImage2String(BufferedImage bufferedImage){
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			ImageIO.write(bufferedImage,"jpg",bos);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(bos.toByteArray());
	}


	public static String getTimeDay(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date);
		rightNow.add(Calendar.MONTH, 1);
		Date dt1 = rightNow.getTime();
		String reStr = sdf.format(dt1);
		return reStr;
	}
	/**
	 * 获取当前时间
	 * @return
	 */
	public static String getCurrentTime() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(new Date());
	}

	public static String toSimple(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return sdf.format(date);
	}

	//获取 充值后的VIP结束时间
	public static Date getTime(Date date, Integer time) {
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date);
		rightNow.add(Calendar.MONTH, time);
		Date dt1 = rightNow.getTime();
		return dt1;
	}

	/**
	 * 日期相减
	 * @param beginDateStr
	 * @param endDateStr
	 * @return
	 */
	public static String getDaySub(Date beginDateStr, Date endDateStr) {
		long day = 0;
		long hour = 0;
		day = (endDateStr.getTime() - beginDateStr.getTime()) / (24 * 60 * 60 * 1000);
		hour = (endDateStr.getTime() - beginDateStr.getTime()) % (24 * 60 * 60 * 1000)/(60*60*1000);
		String surplus=null;
		if (day<0 ||hour<0){
			surplus= "0";
		}else {
			surplus = day +"天" + hour +"小时";
		}
		return surplus;
	}


}
